#include "app_system_link.h"
#include "app_scheduler.h"
#include "app_timer_appsh.h"
#include "sensor_driver_manager.h"
#include "app_timer_process.h"
#include "drv_audio.h"
#include "flash_data.h"
#include "app_timer_process.h"
#include "bluetooth_link.h"
#include "user_speech_recognition.h"


//调度器配置
#define SCHED_MAX_EVENT_DATA_SIZE       24   /**< Maximum size of scheduler events. */
#define SCHED_QUEUE_SIZE                40   /**< Maximum number of events in the scheduler queue. */

static uint8_t *p_buf;
static uint32_t m_flash_write_len;
static uint32_t m_flash_write_block;
static uint32_t m_write_flash_cnt = 0;
static uint8_t m_cur_status = 0;
static void app_system_write_flash(void * p_event_data, uint16_t event_size)
{
    m_write_flash_cnt ++;
    NRF_GPIO->OUTSET = (1UL << 11);
    //flash_data_write(m_flash_write_block,p_buf,m_flash_write_len,0);
    // ble scan影响flash写时间，写flash之前先关闭ble scan
    scan_stop();
    flash_data_store(m_flash_write_block,p_buf,m_flash_write_len,0);
////    scan_start();
    //flash_data_store(m_flash_write_block,p_buf,20,0);
}

static void app_sched_write_flash_callback(int16_t *buff,uint32_t times,uint32_t len)
{
    uint8_t err_code = 0;

    p_buf = (uint8_t *)buff;
    m_flash_write_len = len;
    m_flash_write_block = times - 1;
    app_sched_event_put(NULL, 0, app_system_write_flash);
    //flash_data_write(times - 1,p_buf,len,0);
}

static void app_sched_callback_reg()
{
    bool retb;
    // 定时器，用于擦除SPI_FLASH后，启动PDM
    retb = drv_audio_sched_callback_reg(app_sched_write_flash_callback);
}

void app2_init(void)
{

    APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
    sensor_driver_manager_init(LCD_DISPLAY_RGB_MODE);
    app_timer_process_init();
    
    app_sched_callback_reg();
    user_speech_recogniton_init();

}

